﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using Cesave.DistributedServices.PermisosModule.WindowsHost.Resources;
using Cesave.Domain.PermisosModule.Contracts.Estados;
using Cesave.Domain.PermisosModule.Entities;
using Cesave.Infrastructure.CrossCutting.IoC;

namespace Cesave.DistributedServices.PermisosModule.WindowsHost.ViewModels.Catalogs.Juntas
{
	public partial class JuntasViewModel
	{
		#region Private properties

		/// <summary>
		/// 
		/// </summary>
		private List<Estado> _estados;

		/// <summary>
		/// 
		/// </summary>
		private Estado _estado;

		#endregion

		#region Public properties

		/// <summary>
		/// 
		/// </summary>
		public List<Estado> Estados
		{
			get
			{
				return _estados;
			}
			private set
			{
				_estados = value;
				if (_estados != null)
					RaisePropertyChanged("Estados");
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public Estado Estado
		{
			get
			{
				return _estado;
			}
			private set
			{
				_estado = value;
				if (_estado != null)
					RaisePropertyChanged("Estado");
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public bool IsEstadoReady
		{
			get
			{
				return Estado != null;
			}
		}

		#endregion

		#region Actions

		/// <summary>
		/// 
		/// </summary>
		private void LoadEstados()
		{
			StartWaiting();
			using (BackgroundWorker worker = new BackgroundWorker())
			{
				worker.DoWork += (sender, e) =>
				{
					IEstadoService estadoService = IoCFactory.Resolve<IEstadoService>();
					e.Result = estadoService.FindAllEstados();
				};
				worker.RunWorkerCompleted += (sender, e) =>
				{
					EndWaiting();
					if (!e.Cancelled && e.Error == null)
						Estados = e.Result as List<Estado>;
					else if (e.Error != null)
						MessageBox.Show(
							_juntasView,
							e.Error.Message,
							WindowTitles.JuntasWindowTitle,
							MessageBoxButtons.OK,
							MessageBoxIcon.Error
						);
				};
				worker.WorkerSupportsCancellation = true;
				worker.RunWorkerAsync();
			}
		}

		#endregion

		#region Event methods

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SetCurrentEstado(object sender, EventArgs e)
		{
			BindingSource bindingSource = sender as BindingSource;
			if (bindingSource != null)
				if (bindingSource.Current != null)
					Estado = bindingSource.Current as Estado;
		}

		/// <summary>
		/// 
		/// </summary>
		private void OnEstadosPropertyChanged()
		{
			_juntasView.estadosBindingSource.DataSource = Estados;
			_juntasView.estadosBindingSource.CurrentChanged += SetCurrentEstado;
			SetCurrentEstado(_juntasView.estadosBindingSource, new EventArgs());
		}

		/// <summary>
		/// 
		/// </summary>
		private void OnEstadoPropertyChanged()
		{
			_juntasView.newButton.Enabled = _juntasView.bindingNavigatorAddNewItem.Enabled = IsEstadoReady;
			LoadJuntas();
			LoadMunicipios();
			LoadDistritos();
		}

		#endregion
	}
}
